package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.pojo.dto.CategoryAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.CategoryUpdateDTO;
import cn.tedu.csmall.product.pojo.vo.CategoryListItemVO;
import cn.tedu.csmall.product.pojo.vo.CategoryStandardVO;
import cn.tedu.csmall.product.service.impl.CategoryServiceImpl;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 处理类别相关请求的控制器
 *
 * @author Tan
 * @version 0.0.1
 */
@Slf4j
@RestController
@RequestMapping("/categories")
@Api(tags = "03. 类别模块")
public class CategoryController {
    @Autowired
    CategoryServiceImpl service;

    /**
     * 添加分类
     * @return  添加状态
     */
    //  http://localhost:8080/category/add-new?name=分类测试8&parentId=1&depth=1&keywords=123&sort=1&icon=安顺&enable=1&isParent=4&isDisplay=9
    @PostMapping("/add-new")
    @ApiOperation(value = "添加分类")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(CategoryAddNewDTO dto){
        log.debug("开始处理[添加分类]请求,参数:{}",dto);
            log.debug("添加分类成功");
            service.addNew(dto);
            return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation(value = "根据id删除类别")
    @ApiOperationSupport(order = 201)
    @ApiImplicitParam(name = "id", value = "类别id", required = true, dataType = "long")
    public JsonResult<Void> delete(@PathVariable Long id){
        log.debug("开始处理[删除类别]请求,参数:{}",id);
        service.delete(id);
        return JsonResult.ok();
    }

    @GetMapping("")
    @ApiOperation(value = "查询类别列表")
    @ApiOperationSupport(order = 400)
    public JsonResult<List<CategoryListItemVO>> list(){
        log.debug("开始处理[查询类别列表]请求");
        List<CategoryListItemVO> list = service.list();
        return JsonResult.ok(list);
    }

    @GetMapping("/{parentId}/list-by-parentId")
    @ApiOperation(value = "根据父级id查询类别列表")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<CategoryListItemVO>> listByParentId(@PathVariable Long parentId){
        log.debug("开始处理[根据父级id查询类别列表]请求");
        List<CategoryListItemVO> list = service.listByParentId(parentId);
        return JsonResult.ok(list);
    }

    @GetMapping("/{id}/getStandById")
    @ApiOperation(value = "查询类别详情")
    @ApiOperationSupport(order = 410)
    public JsonResult<CategoryStandardVO> getStandById(@PathVariable Long id){
        log.debug("开始处理[查询类别详情]的请求,参数:{}",id);
        CategoryStandardVO vo = service.getStandardById(id);
        return JsonResult.ok(vo);
    }

    @PostMapping("/{id}/disPlay")
    @ApiOperation(value = "显示类别")
    @ApiOperationSupport(order = 300)
    public JsonResult<Void> disPlay(@PathVariable Long id){
        log.debug("开始处理显示类别的请求,参数:{}",id);
        service.disPlay(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id}/unDisPlay")
    @ApiOperation(value = "隐藏类别")
    @ApiOperationSupport(order = 310)
    public JsonResult<Void> unDisplay(@PathVariable Long id){
        log.debug("开始处理启用类别的请求,参数:{}",id);
        service.unDisPlay(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id}/enable")
    @ApiOperation(value = "将类别状态改为启用")
    @ApiOperationSupport(order = 320)
    public JsonResult<Void> enable(@PathVariable Long id){
        log.debug("开始处理启用类别的请求,参数:{}",id);
        service.enAble(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id}/unEnable")
    @ApiOperation(value = "将类别状态改为未启用")
    @ApiOperationSupport(order = 320)
    public JsonResult<Void> unEnable(@PathVariable Long id){
        log.debug("开始处理显示类别的请求,参数:{}",id);
        service.unEnAble(id);
        return JsonResult.ok();
    }

    @PostMapping("/updateById")
    @ApiOperation(value = "根据id修改类别信息")
    @ApiOperationSupport(order = 320)
    public JsonResult<Void> updateById(CategoryUpdateDTO categoryUpdateDTO){
        log.debug("开始处理修改类别的请求,参数:{}",categoryUpdateDTO);
        service.updateById(categoryUpdateDTO);
        return JsonResult.ok();
    }


}
